results_table <- function(year = "2015", varname = "diet_mean_newspol", weighted = TRUE) {
  dat <- yg15
  weights <- "weights"
  
  if(year == "2016") {
    dat <- yg16
    weights <- "weight"
  }
  if(isTRUE(weighted)) {
  r_all <- dat %>% select(weights = all_of(weights), varname = all_of(varname)) %>%
    summarize(median_all = round(weighted.median(varname, w = weights, na.rm = TRUE), digits = 3),
              mean_all = round(weighted.mean(varname, w = weights, na.rm = TRUE), digits = 3),
              sd_all = round(sqrt(weighted.var(varname, w = weights, na.rm = TRUE)), digits = 3)[1])
  
  r_d <- dat %>% select(weights = all_of(weights), varname = all_of(varname), pid3) %>% 
    filter(pid3 == "Democrat") %>% 
    summarize(median_dem = round(weighted.median(varname, w = weights, na.rm = TRUE), digits = 3),
              mean_dem = round(weighted.mean(varname, w = weights, na.rm = TRUE), digits = 3),
              sd_dem = round(sqrt(weighted.var(varname, w = weights, na.rm = TRUE)), digits = 3)[1])
  
  r_r <- dat %>% select(weights = all_of(weights), varname = all_of(varname), pid3) %>% 
    filter(pid3 == "Republican") %>% 
    summarize(median_rep = round(weighted.median(varname, w = weights, na.rm = TRUE), digits = 3),
              mean_rep = round(weighted.mean(varname, w = weights, na.rm = TRUE), digits = 3),
              sd_rep = round(sqrt(weighted.var(varname, w = weights, na.rm = TRUE)), digits = 3)[1])
  } 
  else {
    r_all <- dat %>% select(varname = all_of(varname)) %>%
      summarize(median_all = round(median(varname, na.rm = TRUE), digits = 3),
                mean_all = round(mean(varname, na.rm = TRUE), digits = 3),
                sd_all = round(sqrt(var(varname, na.rm = TRUE)), digits = 3)[1])
    
    r_d <- dat %>% select(varname = all_of(varname), pid3) %>% 
      filter(pid3 == "Democrat") %>% 
      summarize(median_dem = round(median(varname, na.rm = TRUE), digits = 3),
                mean_dem = round(mean(varname, na.rm = TRUE), digits = 3),
                sd_dem = round(sqrt(var(varname, na.rm = TRUE)), digits = 3)[1])
    
    r_r <- dat %>% select(varname = all_of(varname), pid3) %>% 
      filter(pid3 == "Republican") %>% 
      summarize(median_rep = round(median(varname, na.rm = TRUE), digits = 3),
                mean_rep = round(mean(varname, na.rm = TRUE), digits = 3),
                sd_rep = round(sqrt(var(varname, na.rm = TRUE)), digits = 3)[1])
  }
  
  bind_cols(r_all, r_d, r_r)
}



results_graph <- function(year = "2015", varname = "diet_mean_newspol", weighted = TRUE) {
  dat <- yg15
  weights <- "weights"
  
  if(year == "2016") {
    dat <- yg16
    weights <- "weight"
  }
  if(isTRUE(weighted)) {
    p <- dat %>% select(weights = all_of(weights), varname = all_of(varname), pid3) %>%
      filter(pid3 %in% c("Democrat", "Republican", "Independent")) %>%
               ggplot(aes(x = varname, y = pid3, xmin = -1, xmax = 1, scale = 2.5, 
                          weight = weights/sum(weights))) +
               geom_density_ridges(aes(height=..density.., color = pid3), 
                                   size = 0.7, stat = "density", show.legend = FALSE, na.rm = TRUE) + 
               scale_color_cyclical("", values = c("blue3", "red", "black")) +
               scale_x_continuous("", limits = c(-1, 1)) + 
               scale_y_discrete("", labels = c("Democrats", "Republicans", "Independents")) +
               ggtitle("") + ylab("") + 
               theme_ipsum_rc(axis_text_size = 18, plot_title_size = 24)
  } else {
    p <- dat %>% select(varname = all_of(varname), pid3) %>%
      filter(pid3 %in% c("Democrat", "Republican", "Independent")) %>%
               ggplot(aes(x = varname, y = pid3, xmin = -1, xmax = 1, scale = 2.5)) +
               geom_density_ridges(aes(color = pid3), 
                                   size = 0.7, show.legend = FALSE, na.rm = TRUE) + 
               scale_color_cyclical("", values = c("blue3", "red", "black")) +
               scale_x_continuous("", limits = c(-1, 1)) + 
               scale_y_discrete("", labels = c("Democrats", "Republicans", "Independents")) +
               ggtitle("") + ylab("") + 
               theme_ipsum_rc(axis_text_size = 18, plot_title_size = 24)
  }
  p
}

overlap_coef <- function(year = "2015", varname = "diet_mean_newspol") {
  dat <- yg15
  
  if(year == "2016") {
    dat <- yg16
  }
  if(year == "2016mobile") {
    dat <- yg16mob
  }
  
  ocdat <- dat %>% select(varname = all_of(varname), pid3) %>%
    mutate(diet_mean_radians = scales::rescale(varname, to = c(0, 2*pi)))
  
  oc <- overlapTrue(densityPlot(ocdat$diet_mean_radians[which(!is.na(ocdat$diet_mean_radians) & ocdat$pid3 == "Democrat")])$y, 
                    densityPlot(ocdat$diet_mean_radians[which(!is.na(ocdat$diet_mean_radians) & ocdat$pid3 == "Republican")])$y)
  oc
}
